package com.szl.group.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.szl.group.common.anno.Limit;
import com.szl.group.common.model.R;
import com.szl.group.entity.SysMenuDO;
import com.szl.group.exception.BusinessException;
import com.szl.group.service.ISysMenuService;
import com.szl.group.utils.Assert;
import com.szl.group.web.BaseController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * 系统菜单表 前端控制器
 * </p>
 *
 * @author shenxl
 * @since 2020-06-21
 */
@RestController
@RequestMapping("/admin/sys-menu-do")
public class SysMenuController extends BaseController<ISysMenuService, SysMenuDO> {


    @GetMapping("/list/list")
    public ResponseEntity getList2(SysMenuDO t) {

        // service.test();

        if (true) {
            throw new NullPointerException();
        }


        Assert.function(
                () -> 1 + 0,
                new BusinessException(400, "err"));

        List<SysMenuDO> list = Assert.function(
                () -> service.list(new QueryWrapper<>(t)),
                400, "what");

        return ResponseEntity.ok(R.of(list));
    }

    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

    @Limit(key = "test", period = 100, count = 10)
    @GetMapping("/test")
    public int testLimiter() {
        // 意味著 100S 内最多允許訪問10次
        return ATOMIC_INTEGER.incrementAndGet();
    }

}
